package com.wskj.web.controller.workbench;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.annotation.Log;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.domain.entity.SysUser;
import com.wskj.common.core.domain.entity.TbUserTiktok;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.system.service.ISysRoleService;
import com.wskj.system.service.ISysUserService;
import com.wskj.workbench.service.ITbUserTiktokService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 查询通过微信小程序注册进来的用户
 **/
@RestController
@RequestMapping("/yshrjg/user")
@AllArgsConstructor
public class TbUserController extends BaseController {

    private final ISysUserService userService;

    private final ISysRoleService roleService;

    private final ITbUserTiktokService userTiktokService;


    @GetMapping("/list")
    public TableDataInfo<SysUser> list(SysUser user) {
        return userService.selectPageNotSysUserList(user);
    }

    /**
     * 查询某一个用户下的子用户列表
     **/
    @GetMapping("/children/list")
    public TableDataInfo<SysUser> childrenList(SysUser user) {
        return userService.childrenListPage(user);
    }



    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('yshrjg:user:query')")
    @GetMapping(value = {"/", "/{userId}"})
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        Map<String, Object> ajax = new HashMap<>();
        if (Validator.isNotNull(userId)) {
            SysUser sysUser = userService.selectUserById(userId);
            // 查询抖音详情
            List<TbUserTiktok> list = userTiktokService.list(Wrappers.lambdaQuery(TbUserTiktok.class).eq(TbUserTiktok::getUserId, userId));
            sysUser.setDetailList(list);
            ajax.put("user", sysUser);
            ajax.put("roleIds", roleService.selectRoleListByUserId(userId));
        }
        return AjaxResult.success(ajax);
    }




    /**
     * 共享股分红名额
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "共享股分红名额", businessType = BusinessType.UPDATE)
    @PutMapping("/changeContributeQuota")
    public AjaxResult changeContributeQuota(@RequestBody SysUser user) {
        return toAjax(userService.updateUserStatus(user));
    }

}
